package model

import (
	"context"
	"github.com/SupenBysz/gf-admin-community/utility/daoctl"
	"github.com/SupenBysz/gf-admin-community/utility/daoctl/dao_interface"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
)

//考试成绩
type Examination struct {
	Id                string  `json:"id"                description:"ID"`
	Name              string `json:"name"              v:"required|max-length:128#名称不能为空|名称长度超128字符出限定范围" description:"团队名称，公司维度下唯一"`
	ClassNo		string		`json:"classNo"       v:"required#班级号校验失败" description:"班级号"`
	Fraction string  `json:"fraction" v:"required#分数校验失败" description:"分数"`
	EmployeeId string `json:"employeeId" v:"required#用户Id校验失败" description:"员工id"`
}

type ExaminationDao struct {
	table   string      // table is the underlying table name of the DAO.
	group   string      // group is the database configuration group name of current DAO.
	columns Examination // columns contains all the column names of Table for convenient usage.
}

type ExaminationList []Examination

var examinationColumns = Examination{
	Id:         "id",
	Name:       "name",
	ClassNo:    "class_no",
	Fraction:   "fraction",
	EmployeeId: "employee_id",
}

// NewExaminationDao creates and returns a new DAO object for table data access.
func NewExaminationDao(proxy ...dao_interface.IDao) *ExaminationDao {
	var dao *ExaminationDao
	if len(proxy) > 0 {
		dao = &ExaminationDao{
			group:   proxy[0].Group(),
			table:   proxy[0].Table(),
			columns: examinationColumns,
		}
		return dao
	}

	return &ExaminationDao{
		group:   "default",
		table:   "ttt_examination",
		columns: examinationColumns,
	}
}

// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *ExaminationDao) DB() gdb.DB {
	return g.DB(dao.group)
}

// Table returns the table name of current dao.
func (dao *ExaminationDao) Table() string {
	return dao.table
}

// Group returns the configuration group name of database of current dao.
func (dao *ExaminationDao) Group() string {
	return dao.group
}

// Columns returns all column names of current dao.
func (dao *ExaminationDao) Columns() Examination {
	return dao.columns
}

// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *ExaminationDao) Ctx(ctx context.Context, cacheOption ...*gdb.CacheOption) *gdb.Model {
	return dao.DaoConfig(ctx, cacheOption...).Model
}

func (dao *ExaminationDao) DaoConfig(ctx context.Context, cacheOption ...*gdb.CacheOption) dao_interface.DaoConfig {
	daoConfig := dao_interface.DaoConfig{
		Dao:   dao,
		DB:    dao.DB(),
		Table: dao.table,
		Group: dao.group,
		Model: dao.DB().Model(dao.Table()).Safe().Ctx(ctx),
	}

	if len(cacheOption) == 0 {
		daoConfig.CacheOption = daoctl.MakeDaoCache(dao.Table())
		daoConfig.Model = daoConfig.Model.Cache(*daoConfig.CacheOption)
	} else {
		if cacheOption[0] != nil {
			daoConfig.CacheOption = cacheOption[0]
			daoConfig.Model = daoConfig.Model.Cache(*daoConfig.CacheOption)
		}
	}

	daoConfig.Model = daoctl.RegisterDaoHook(daoConfig.Model)

	return daoConfig
}

// Transaction wraps the transaction logic using function f.
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *ExaminationDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
	return dao.Ctx(ctx).Transaction(ctx, f)
}